package service

import (
	"ruankepaiming-server/model"
	"ruankepaiming-server/model/request"
	"ruankepaiming-server/pkg/global"
)

func GetZongbangPage(page int ,limit int , dataS request.Zongbangreq)( []model.Zongbang,int64 ,error ){
	var datas []model.Zongbang
	var total int64
	temp := global.DB.Model(&model.Zongbang{}).Order("id asc")

	if dataS.ZongbangProvince!= ""{
		temp = temp.Where("zongbang_province like ?","%"+dataS.ZongbangProvince+"%")

	}
	if dataS.ZongbangSchoolType != ""{
		temp = temp.Where("zongbang_school_type like ?","%"+dataS.ZongbangSchoolType+"%")

	}
	if dataS.ZongbangSchoolName != ""{
		temp = temp.Where("zongbang_school_name like ?","%"+dataS.ZongbangSchoolName+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetZhubangPage(page int ,limit int , dataS request.Zhubangreq)( []model.Zhubang,int64 ,error ){
	var datas []model.Zhubang
	var total int64
	temp := global.DB.Model(&model.Zhubang{}).Order("id asc")
	if dataS.ZhubangProvince!= ""{
		temp = temp.Where("zhubang_province like ?","%"+dataS.ZhubangProvince+"%")

	}
	if dataS.ZhubangSchoolType != ""{
		temp = temp.Where("zhubang_school_type like ?","%"+dataS.ZhubangSchoolType+"%")

	}
	if dataS.ZhubangSchoolName != ""{
		temp = temp.Where("zhubang_school_name like ?","%"+dataS.ZhubangSchoolName+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetMedicalPage(page int ,limit int , dataS request.Medicalreq)( []model.Medical,int64 ,error ){
	var datas []model.Medical
	var total int64
	temp := global.DB.Model(&model.Medical{}).Order("id asc")
	if dataS.MedicalProvince!= ""{
		temp = temp.Where("medical_province like ?","%"+dataS.MedicalProvince+"%")

	}
	if dataS.MedicalSchoolType != ""{
		temp = temp.Where("medical_school_type like ?","%"+dataS.MedicalSchoolType+"%")

	}
	if dataS.MedicalSchoolName != ""{
		temp = temp.Where("medical_school_name like ?","%"+dataS.MedicalSchoolName+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetFinancePage(page int ,limit int , dataS request.Financereq)( []model.Finance,int64 ,error ){
	var datas []model.Finance
	var total int64
	temp := global.DB.Model(&model.Finance{}).Order("id asc")
	if dataS.FinanceProvince!= ""{
		temp = temp.Where("finance_province like ?","%"+dataS.FinanceProvince+"%")

	}
	if dataS.FinanceSchoolName != ""{
		temp = temp.Where("finance_school_type like ?","%"+dataS.FinanceSchoolName+"%")

	}
	if dataS.FinanceSchoolName != ""{
		temp = temp.Where("finance_school_name like ?","%"+dataS.FinanceSchoolName+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}
func GetLanguagePage(page int ,limit int , dataS request.Languagereq)( []model.Language,int64 ,error ){
	var datas []model.Language
	var total int64
	temp := global.DB.Model(&model.Language{}).Order("id asc")
	if dataS.LanguageProvince!= ""{
		temp = temp.Where("language_province like ?","%"+dataS.LanguageProvince+"%")

	}
	if dataS.LanguageSchoolName != ""{
		temp = temp.Where("language_school_name like ?","%"+dataS.LanguageSchoolName+"%")

	}
	if dataS.LanguageSchoolType!= ""{
		temp = temp.Where("language_school_type like ?","%"+dataS.LanguageSchoolType+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetLawPage(page int ,limit int , dataS request.Lawreq)( []model.Law,int64 ,error ){
	var datas []model.Law
	var total int64
	temp := global.DB.Model(&model.Law{}).Order("id asc")
	if dataS.LawProvince!= ""{
		temp = temp.Where("law_province like ?","%"+dataS.LawProvince+"%")

	}
	if dataS.LawSchoolName != ""{
		temp = temp.Where("law_school_name like ?","%"+dataS.LawSchoolName+"%")

	}
	if dataS.LawSchoolType!= ""{
		temp = temp.Where("law_school_type like ?","%"+dataS.LawSchoolType+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetMinzuPage(page int ,limit int , dataS request.Minzureq)( []model.Minzu,int64 ,error ){
	var datas []model.Minzu
	var total int64
	temp := global.DB.Model(&model.Minzu{}).Order("id asc")
	if dataS.MinzuProvince!= ""{
		temp = temp.Where("minzu_province like ?","%"+dataS.MinzuProvince+"%")

	}
	if dataS.MinzuSchoolName != ""{
		temp = temp.Where("minzu_school_type like ?","%"+dataS.MinzuSchoolName+"%")

	}
	if dataS.MinzuSchoolType!= ""{
		temp = temp.Where("minzu_school_name like ?","%"+dataS.MinzuSchoolType+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetTiyuPage(page int ,limit int , dataS request.Tiyureq)( []model.Tiyu,int64 ,error ){
	var datas []model.Tiyu
	var total int64
	temp := global.DB.Model(&model.Tiyu{}).Order("id asc")
	if dataS.TiyuProvince!= ""{
		temp = temp.Where("tiyu_province like ?","%"+dataS.TiyuProvince+"%")

	}
	if dataS.TiyuSchoolName != ""{
		temp = temp.Where("tiyu_school_type like ?","%"+dataS.TiyuSchoolName+"%")

	}
	if dataS.TiyuSchoolType!= ""{
		temp = temp.Where("tiyu_school_name like ?","%"+dataS.TiyuSchoolType+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetHezuoPage(page int ,limit int , dataS request.Hezuoreq)( []model.Hezuo,int64 ,error ){
	var datas []model.Hezuo
	var total int64
	temp := global.DB.Model(&model.Hezuo{}).Order("id asc")
	if dataS.HezuoProvince!= ""{
		temp = temp.Where("hezuo_province like ?","%"+dataS.HezuoProvince+"%")

	}
	if dataS.HezuoSchoolName != ""{
		temp = temp.Where("hezuo_school_type like ?","%"+dataS.HezuoSchoolName+"%")

	}
	if dataS.HezuoSchoolType!= ""{
		temp = temp.Where("hezuo_school_name like ?","%"+dataS.HezuoSchoolType+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetMinbanPage(page int ,limit int , dataS request.Minbanreq)( []model.Minban,int64 ,error ){
	var datas []model.Minban
	var total int64
	temp := global.DB.Model(&model.Minban{}).Order("id asc")
	if dataS.MinbanProvince!= ""{
		temp = temp.Where("minban_province like ?","%"+dataS.MinbanProvince+"%")

	}
	if dataS.MinbanSchoolName != ""{
		temp = temp.Where("minban_school_type like ?","%"+dataS.MinbanSchoolName+"%")

	}
	if dataS.MinbanSchoolType!= ""{
		temp = temp.Where("minban_school_type like ?","%"+dataS.MinbanSchoolType+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}

func GetYishuPage(page int ,limit int , dataS request.Yishureq)( []model.Yishu,int64 ,error ){
	var datas []model.Yishu
	var total int64
	temp := global.DB.Model(&model.Yishu{}).Order("id asc")
	if dataS.YishuProvince!= ""{
		temp = temp.Where("yishun_province like ?","%"+dataS.YishuProvince+"%")

	}
	if dataS.YishuSchoolName != ""{
		temp = temp.Where("yishu_school_type like ?","%"+dataS.YishuSchoolName+"%")

	}
	if dataS.YishuSchoolType!= ""{
		temp = temp.Where("yishu_school_name like ?","%"+dataS.YishuSchoolType+"%")

	}

	err := temp.Count(&total).Limit(limit).Offset((page-1)*limit).Find(&datas).Error
	if err != nil {
		return nil,0,err
	}
	return  datas,total,nil
}